//package com.joker.aigc.bi.utils;
//
//
//import cn.hutool.core.lang.Assert;
//import com.alicp.jetcache.Cache;
//import com.alicp.jetcache.CacheManager;
//import com.alicp.jetcache.anno.CacheType;
//import com.alicp.jetcache.template.QuickConfig;
//import com.joker.aigc.bi.domain.po.DatabaseInfoPO;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.time.Duration;
//
///**
// * @description:
// * @author: feixiang.li
// * @date: 2025-01-17 17:19
// */
//@Component
//@Slf4j
//@RequiredArgsConstructor
//public class SqlFactoryUtils {
//
//    private final CacheManager cacheManager;
//
//    /**
//     * 连接吃缓存
//     */
//    private Cache<Long, DataSource> dataSourceCache;
//
//    @PostConstruct
//    public void init() {
//        QuickConfig qc = QuickConfig.newBuilder("dataBaseCache:")
//                .expire(Duration.ofSeconds(36000))
//                .cacheType(CacheType.LOCAL)
//                // 本地缓存更新后，将在所有的节点中删除缓存，以保持强一致性
//                .syncLocal(false)
//                .build();
//        dataSourceCache = cacheManager.getOrCreateCache(qc);
//    }
//
//    DataSource getDataSource(DatabaseInfoPO databaseInfoPO) {
//        Assert.notNull(databaseInfoPO, "参数不能为空");
//        Assert.notNull(databaseInfoPO.getId(), "数据库id不能为空");
//        // 先从缓存中获取数据源
//        if (dataSourceCache.get(databaseInfoPO.getId()) != null) {
//            return dataSourceCache.get(databaseInfoPO.getId());
//        }
//        // 配置 Druid 数据库连接池
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUrl("jdbc:mysql://" + databaseInfoPO.getIp() + ":" + databaseInfoPO.getPort() + "/" +
//                databaseInfoPO.getDatabaseName() + "?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
//        dataSource.setUsername(databaseInfoPO.getUsername());
//        dataSource.setPassword(databaseInfoPO.getPassword());
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        // 配置连接池的一些参数
//        dataSource.setInitialSize(5);
//        dataSource.setMinIdle(5);
//        dataSource.setMaxActive(20);
//
//        //        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
////        // 如何使用druid 连接池
////        dataSource = DruidDataSourceFactory;
////        dataSource.setUrl("jdbc:mysql://101.201.104.148:3306/aigc?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
////        dataSource.setUsername("jiema");
////        dataSource.setPassword("123qweQWE");
////        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
////        dataSource.setInitialSize(1);
////        dataSource.setMaxActive(10);
////        dataSource.setMinIdle(1);
////        dataSource.setMaxWait(60000);
////        dataSource.setTimeBetweenEvictionRunsMillis(60000);
////        dataSource.setMinEvictableIdleTimeMillis(300000);
//        dataSourceCache.put(databaseInfoPO.getId(), dataSource);
//        return dataSource;
//    }
//
////    public static void main(String[] args) {
////        DatabaseInfoPO databaseInfoPO = new DatabaseInfoPO();
////        databaseInfoPO = new DatabaseInfoPO();
////        databaseInfoPO.setIp("rm-uf682626j42h0nn6ljo.mysql.rds.aliyuncs.com");
////        databaseInfoPO.setPort("3306");
////        databaseInfoPO.setUsername("jiema");
////        databaseInfoPO.setPassword("123qweQWE");
////        databaseInfoPO.setDatabaseName("aigc");
////        databaseInfoPO.setType(1);
////
////        DataSource dataSource = getDataSource(databaseInfoPO);
////        // 创建 Druid 数据源
////        try (Connection connection = dataSource.getConnection()) {
////            // 执行数据库操作
////            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM account_book limit 100");
////            ResultSet resultSet = preparedStatement.executeQuery();
////            while (resultSet.next()) {
////                // 处理查询结果
////                log.info(resultSet.getString("name"));
////            }
////        } catch (SQLException e) {
////            log.error("查询数据库失败", e);
////        } finally {
////            // 关闭 Druid 数据源
////
////        }
////    }
//
//
//    public static Connection getConnection(DataSource dataSource) {
//
//        try {
//            //获取连接
//            return dataSource.getConnection();
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * @param connection 把连接的对象作为参数
//     * @return void
//     * @methodName closeRes
//     * @Description 关闭连接
//     * @Date 2023/6/13 9:28
//     */
//    public static void closeResource(Connection connection, PreparedStatement preparedStatement) {
//        //后开先关
//        try {
//            if (preparedStatement != null) {
//                preparedStatement.close();
//            }
//            if (connection != null) {
//                connection.close();
//            }
//
//        } catch (SQLException sq) {
//            sq.printStackTrace();
//        }
//
//    }
//
//    public static void closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
//        //后开先关
//        try {
//            if (resultSet != null) {
//                resultSet.close();
//            }
//            if (preparedStatement != null) {
//                preparedStatement.close();
//            }
//            if (connection != null) {
//                connection.close();
//            }
//
//        } catch (SQLException sq) {
//            log.error("关闭连接失败", sq);
//        }
//
//    }
//}
